package com.gxa.eshops.admin.realm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxa.eshops.admin.service.impl.AdminBOServiceImpl;
import com.gxa.eshops.admin.service.impl.RoleBOServiceImpl;
import com.gxa.eshps.cores.pojo.bo.AdminBO;
import com.gxa.eshps.cores.pojo.bo.RoleBo;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: Mr.Wang
 * @create: 2022-06-28 16:14
 **/
@Component
public class AdminRealm extends AuthorizingRealm {
    @Autowired
    RoleBOServiceImpl roleBOServiceImpl;
    @Autowired
    AdminBOServiceImpl adminBOService;

    /**
     * 授权
     *
     * @param principalCollection 主要收集
     * @return {@code AuthorizationInfo}
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //登录用户名
        String phone = (String) principalCollection.getPrimaryPrincipal();
        //添加角色和权限
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //查询角色信息 roleList
        LambdaQueryWrapper<RoleBo> lambda = Wrappers.lambdaQuery(RoleBo.class);
        lambda.eq(RoleBo::getPhone, phone);
        List<RoleBo> list = roleBOServiceImpl.list(lambda);
        for (RoleBo roleBo : list) {
            //查询角色名,只有一个,不用遍历
            info.addRole(roleBo.getName());
            //查询权限并授权
            LambdaQueryWrapper<AdminBO> adminBOLambda = new LambdaQueryWrapper<>();
            adminBOLambda.eq(AdminBO::getUsername, roleBo.getName());
            List<AdminBO> adminBOList = adminBOService.list(adminBOLambda);
            for (AdminBO adminBO : adminBOList) {
                info.addStringPermission(adminBO.getName());
            }
        }
        //返回已经完成绑定(角色信息和授权信息)的授权信息对象
        return info;
    }

    /**
     * 身份验证
     * @param authenticationToken 身份验证令牌
     * @return {@code AuthenticationInfo}
     * @throws AuthenticationException 身份验证异常
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取用户手机号
        String phone = (String) authenticationToken.getPrincipal();
        LambdaQueryWrapper<RoleBo> lambda = Wrappers.lambdaQuery(RoleBo.class);
        //登录验证
        lambda.eq(RoleBo::getPhone, phone);
        RoleBo roleBo = roleBOServiceImpl.getOne(lambda);
        return new SimpleAuthenticationInfo(phone, roleBo.getPwd(), getName());
    }
}
